import * as wx from './src/wx'
import { getEvn } from '@/utils/h5/handleEvn'
import * as utils from '@/utils/index'

import msg from '@/utils/message'
// import config from '@/config'
import GDMap from '@/utils/h5/map'
import BMap from '@/utils/h5/bMap'

const EVN = getEvn()

export const callPhone = (tel) => {
  window.location.href = `tel:${tel}`
}

// 获取经纬度
export const getLocation = () => {
  const url = window.location.href
  if (url.includes('GD=1')) {
    // 强制开启高德定位
    console.log('强制开启高德定位')
    return GDMap.geolocation()
  }
  if (EVN === 'wx') {
    return wx.getLocation()
  } else {
    return BMap.geolocation()
  }
}

export const scanQRCode = (needResult) => {
  if (EVN === 'wx') {
    return wx.wxScanQRCode(needResult)
  } else {
    msg.toast('该环境不支持扫描功能')
    return Promise.reject({ msg: '该环境不支持扫描功能' })
  }
}

// 是否可用扫码
export const hasQrCode = () => {
  return ['wx', 'boc', 'icbc'].includes(EVN)
}

// “分享给朋友”及“分享到QQ”
export const updateAppMessageShareData = (data = {}) => {
  if (EVN === 'wx') {
    return wx.wxUpdateAppMessageShareData(data)
  } else {
    return Promise.reject({ msg: '该环境不支持分享给朋友' })
  }
}

// 自定义“分享到朋友圈”及“分享到QQ空间”按钮的分享内容
export const updateTimelineShareData = (data = {}) => {
  if (EVN === 'wx') {
    return wx.wxUpdateTimelineShareData(data)
  } else {
    return Promise.reject({ msg: '该环境不支持分享到朋友圈' })
  }
}

// 关闭页面
export const close = (cb) => {
  if (EVN === 'wx') {
    return wx.close(cb)
  } else {
    // 正常浏览器
    window.close && window.close()
  }
}

// 修改标题
export const setTitle = (ttl) => {
  document.title = ttl
}

// 复制
export const copy = (value) => {
  return utils.copy(value).then((res) => {
    // msg.showMsgSuccess('复制成功')
    msg.toast('复制成功')
  })
}
